package com.yangguang.huahui.ui.activitys;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yangguang.huahui.Config;
import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.entity.AttentionList;
import com.yangguang.huahui.mvp.presenter.MiaomuPresenter;
import com.yangguang.huahui.mvp.view.MiaomuView;
import com.yangguang.huahui.ui.adapters.MiaomuAdapter;
import com.yangguang.huahui.ui.adapters.OnItemClickListener;
import com.yangguang.huahui.ui.adapters.PinzhongAdapter;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.widget.ProgressActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class FindMiaomuActivity extends BaseMvpActivity<MiaomuView, MiaomuPresenter> implements MiaomuView, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_search)
    ImageView iv_search;
    private int page;
    private String goods_type = "";
    private String variety = "";
    @BindView(R.id.mProgressActivity)
    ProgressActivity mProgressActivity;
    @BindView(R.id.mSwip)
    SwipeRefreshLayout mSwip;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private LinearLayoutManager layoutManager;
    private MiaomuAdapter mAdapter;

    @BindView(R.id.mLL_select_type_1)
    LinearLayout mLL_select_type_1;
    @BindView(R.id.mLL_select_type_2)
    LinearLayout mLL_select_type_2;

    @OnClick({R.id.iv_back, R.id.mLL_more, R.id.mLL_right, R.id.mLL_kind})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.mLL_more:
                mLL_select_type_1.setVisibility(View.GONE);
                mLL_select_type_2.setVisibility(View.VISIBLE);
                mLL_select_type_1.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_exit));
                mLL_select_type_2.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_enter));
                break;
            case R.id.mLL_right:
                mLL_select_type_1.setVisibility(View.VISIBLE);
                mLL_select_type_2.setVisibility(View.GONE);
                mLL_select_type_1.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_enter));
                mLL_select_type_2.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_exit));
                break;
            case R.id.mLL_kind:
                if (flowersType != null)
                selectPinlei("种类");
                break;
        }
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_find_miaomu;
    }

    @Override
    protected void initView() {
        getFlowers_type();
        tv_title.setText(getString(R.string.miaomu_list));
        iv_search.setVisibility(View.VISIBLE);
        mSwip.setColorSchemeResources(R.color.main_color, R.color.main_color, R.color.main_color);
        mSwip.setOnRefreshListener(this);
        layoutManager = new LinearLayoutManager(mContext);
        mRecyclerView.setLayoutManager(layoutManager);
        //实现首次自动显示加载提示
        mSwip.post(new Runnable() {
            @Override
            public void run() {
                mSwip.setRefreshing(true);
            }
        });
        mAdapter = new MiaomuAdapter(R.layout.find_miaomu_list_item, null);
        //设置加载动画
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        //将适配器添加到RecyclerView
        mRecyclerView.setAdapter(mAdapter);
        //设置自动加载监听
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(mContext, DetailActivity.class)
                        .putExtra("webUrl", Config.SUPPLY_DETAIL)
                        .putExtra("id", mAdapter.getItem(position).getId())
                        .putExtra("type", 8)
                        .putExtra("detail_type", "2"));
            }
        });
    }

    @Override
    protected void initData() {

    }
    @Override
    protected MiaomuPresenter initPresenter() {
        return new MiaomuPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getSeedlings(page, goods_type, variety);
    }

    @Override
    public void onSuccess(AttentionList result) {
        if (result.getList() != null){
            if (page > 0){
                mAdapter.addData(result.getList());//新增数据
                mAdapter.notifyDataChangedAfterLoadMore(true);
            }else {
                mAdapter.setNewData(result.getList());
                mAdapter.openLoadMore(result.getList().size(),true);//设置是否可以下拉加载  以及加载条数
            }
        }else {
            if (page > 0) {
                //所有数据加载完成后显示
                try {
                    mAdapter.notifyDataChangedAfterLoadMore(false);
                    View view = getLayoutInflater().inflate(R.layout.layout_end_view, (ViewGroup) mRecyclerView.getParent(), false);
                    mAdapter.addFooterView(view);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                toEmpty();
            }
        }
        mSwip.setRefreshing(false);
    }
    public void toEmpty(){
        mProgressActivity.showEmpty(getResources().getDrawable(R.mipmap.load_no_data),"暂无数据","努力收集中");
    }

    @Override
    public void onError() {
        mSwip.setRefreshing(false);
        toError();
    }
    public void toError() {
        mProgressActivity.showError(getResources().getDrawable(R.mipmap.load_error), getString(R.string.progressActivityErrorTitlePlaceholder),
                getString(R.string.progressActivityErrorContentPlaceholder), getString(R.string.progressActivityErrorButton), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mProgressActivity.showContent();
                        //重试
                        onRefresh();
                    }
                });
    }

    @Override
    public void onRefresh() {
        page = 0;
        fetchData();
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        fetchData();
    }

    private int fistPosition = -1;
    private int seconPosition = -1;
    private int thirdPosition = -1;
    private String yiji_id = "";
    private String erji_id = "";
    private String erji_name = "";
    private String sanji_id = "";
    private String sanji_name = "";

    private void selectPinlei(String title) {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.area_select, null);
        TextView tv_title = (TextView) contentView.findViewById(R.id.tv_input_title);
        tv_title.setText(title);
        ListView lv_first = (ListView) contentView.findViewById(R.id.lv_first);
        final ListView lv_second = (ListView) contentView.findViewById(R.id.lv_second);
        final ListView lv_third = (ListView) contentView.findViewById(R.id.lv_third);
        PinzhongAdapter pinzhongAdapter = new PinzhongAdapter(mContext, flowersType.getList(), R.layout.select_area_list_item,
                lv_second, lv_third, fistPosition, seconPosition, thirdPosition);
        lv_first.setAdapter(pinzhongAdapter);
        final AlertDialog alertDialog = show_dialog_max(contentView);
        pinzhongAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                yiji_id = id;
            }
        });
        PinzhongAdapter.SecondAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                erji_id = id;
                erji_name = name;
                sanji_id = "";
                sanji_name = "";
            }
        });
        PinzhongAdapter.SecondAdapter.ThirdAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                sanji_id = id;
                sanji_name = name;
                goods_type = id;
            }
        });
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.iv_close:
                        alertDialog.dismiss();
                        break;
                    case R.id.iv_ok:
                        if (lv_second.getAdapter() != null
                                && CommonUtil.isNull(erji_id)) {
                            CommonUtil.myToastA(mContext, "请选择第二类");
                            return;
                        }
                        goods_type = erji_id;
                        variety = sanji_id;
                        onRefresh();
                        alertDialog.dismiss();
                        break;
                }
            }
        };
        contentView.findViewById(R.id.iv_close).setOnClickListener(listener);
        contentView.findViewById(R.id.iv_ok).setOnClickListener(listener);
    }
}
